package com.wulis.config.security;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.wulis.common.model.enums.EnableStateEnum;

import lombok.Data;

/**
 * @author WuliBao
 */
@Data
public class AuthUserDetails implements UserDetails, Serializable {
    /**
     * 账号ID
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long accountId;
    
    /**
     * 账号
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private String account;
    
    /**
     * 密码
     */
    private String password;
    
    /**
     * 启用状态：0关闭 1启用
     */
    private Integer enable;
    
    /**
     * 角色
     */
    private Set<Role> roles;
    
    /**
     * 用户信息
     */
    private UserInfo userInfo;
    
    @Data
    public static class UserInfo {
        /**
         * 用户ID
         */
        private Long userId;
        
        /**
         * 所属部门ID
         */
        private Long departmentId;
        
        /**
         * 所属机构ID
         */
        private Long organizationId;
        
        /**
         * 用户名称
         */
        private String userName;
        
        /**
         * 电话号码
         */
        private String phoneNumber;
    }
    
    @Data
    public static class Role implements GrantedAuthority {
        /**
         * 角色ID
         */
        private Long roleId;
        
        /**
         * 角色名称
         */
        private String roleName;
        
        /**
         * 角色唯一标记
         */
        private String roleMark;
        
        @Override
        public String getAuthority() {
            return roleMark;
        }
        
    }
    
    public AuthUserDetails() {
        this.userInfo = new UserInfo();
        this.roles = new HashSet<>();
    }
    
    @Override
    @JSONField(serialize = false)
    public String getPassword() {
        return this.password;
    }
    
    @Override
    @JSONField(serialize = false)
    public String getUsername() {
        return this.userInfo.getUserName();
    }
    
    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonExpired() {
        return true;
    }
    
    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonLocked() {
        return true;
    }
    
    @Override
    @JSONField(serialize = false)
    public boolean isCredentialsNonExpired() {
        return true;
    }
    
    @Override
    @JSONField(serialize = false)
    public boolean isEnabled() {
        return EnableStateEnum.IS_ENABLED.value().equals(this.enable);
    }
    
    @Override
    @JSONField(serialize = false)
    public Set<? extends GrantedAuthority> getAuthorities() {
        return this.roles;
    }
    
    /**
     * 判断当前用户对象是否为空
     * 
     * @return boolean
     */
    @JSONField(serialize = false)
    public boolean isEmpty() {
        return this.accountId == null;
    }
}
